﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SeparateProjects
{
    public partial class Pro_User_ChangePassword : Form
    {
        public Pro_User_ChangePassword()
        {
            InitializeComponent();
        }
        public Pro_User_ChangePassword(String AccountNumber)
        {
            InitializeComponent();
            txt_USerNumber.Text = AccountNumber;
            this.txt_USerNumber.Enabled = false;
        }
        #region   修改密码
        /// <summary>
        /// 注册验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ConfirmationChanges_Click(object sender, EventArgs e)
        {
            CheckPassword();
            var UserName = this.txt_USerNumber.Text.Trim();
            var OldPassword = this.txt_OldPassword.Text.Trim();
            var NewPassword = this.txt_NewPassword.Text.Trim();
            var Confirm = this.txt_ConfirmPassword.Text.Trim();
            string str = @"Data Source=DESKTOP-28VSNS5;
				        Initial Catalog=Training_Project;
				        Uid=Lv;
				        Pwd=123456";
            var coon = new SqlConnection(str);
            try
            {
                coon.Open();
                var sql = string.Format(@"select Password from Stu_Login
                                            where UserName = '{0}'", UserName);
                var comm = new SqlCommand(sql, coon);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    var PassWord = dr["Password"];
                    if (PassWord.Equals(OldPassword))
                    {
                        Change();
                    }
                    else
                    {
                        MessageBox.Show("输入原密码与登录密码不匹配，请重新输入", "提示信息",
                                MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    }
                }
                dr.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器异常，请联系管理员" + ex.Message);
            }
            finally
            {
                coon.Close();
            }
        }
        #endregion
        #region  修改密码
        public void Change()
        {
            var UserName = this.txt_USerNumber.Text.Trim();
            var OldPassword = this.txt_OldPassword.Text.Trim();
            var NewPassword = this.txt_NewPassword.Text.Trim();
            var Confirm = this.txt_ConfirmPassword.Text.Trim();
            if (NewPassword.Equals(Confirm))
            {
                string str = @"Data Source=DESKTOP-28VSNS5;
				        Initial Catalog=Training_Project;
				        Uid=Lv;
				        Pwd=123456";
                var coon = new SqlConnection(str);
                try
                {
                    coon.Open();
                    var sql = string.Format(@"update Stu_Login set Password ='{0}'
                                           where UserName ='{1}'", Confirm, UserName);
                    var comm = new SqlCommand(sql, coon);
                    int number = comm.ExecuteNonQuery();
                    if (number > 0)
                    {
                        DialogResult dr = MessageBox.Show("更新成功", "提示信息",
                                                            MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                        if (dr.Equals(DialogResult.OK))
                        {
                            this.Hide();
                        }
                    }
                    else
                    {
                        MessageBox.Show("更新失败", "提示信息",
                                                            MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("服务器异常，请联系管理员" + ex.Message);
                }
                finally
                {
                    coon.Close();
                }
            }
        }
        #endregion
        #region   验证修改密码是否合法
        public void CheckPassword()
        {
            if (String.IsNullOrWhiteSpace(txt_OldPassword.Text.Trim()))
            {
                MessageBox.Show("输入原密码不能为空", "提示信息",
                                MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_OldPassword.Focus();
            }
            else if (String.IsNullOrWhiteSpace(txt_NewPassword.Text.Trim()))
            {
                MessageBox.Show("输入新密码不能为空", "提示信息",
                               MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_NewPassword.Focus();
            }
            else if (String.IsNullOrWhiteSpace(txt_ConfirmPassword.Text.Trim()))
            {
                MessageBox.Show("输入确认密码不能为空", "提示信息",
                               MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_ConfirmPassword.Focus();
            }
        }
        #endregion
        #region   退出按钮
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定退出？", "提示信息",
                                                MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dr.Equals(DialogResult.Yes))
            {
                this.Hide();
            }
        }
        #endregion
    }
}
